﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.Business.MVC;
using UMLTouch.DomainRecognition;
using UMLTouch.Core.UI;

namespace UMLTouch.Business
{
    public class UMLTouchModel : IUMLTouchModel
    {
        public event EventHandler<ItemAddedEventArgs> ItemRecognized;
        public event EventHandler<DiagramContextAddedEventArgs> DiagramContextAdded;
        public event EventHandler<UnknownItemAddedEventArgs> OnUnknownItemDetected;

        List<IUMLTouchView> _views;
        List<IDiagramContext> _contexts;

        public IEnumerable<IUMLTouchView> IUMLTouchView
        {
            get { return _views; }
        }

        public IEnumerable<IDiagramContext> IDiagramContext
        {
            get { return _contexts; }
        }

        public void RegisterView(IUMLTouchView view)
        {
            if (view != null)
            {
                _views.Add(view);
            }
        }
        public void RegisterContext(IDiagramContext context)
        {
            if (context != null)
            {
                _contexts.Add(context);
                context.OnItemRecognized += new EventHandler<ItemAddedEventArgs>(context_OnItemRecognized);
                context.OnUnknownItemDetected += new EventHandler<UnknownItemAddedEventArgs>(context_OnUnknownItemDetected);
                DiagramContextAddedEventArgs args = new DiagramContextAddedEventArgs(context);
                if (DiagramContextAdded != null)
                {
                    DiagramContextAdded(this, args);
                }
            }
        }

        void context_OnUnknownItemDetected(object sender, UnknownItemAddedEventArgs e)
        {
            if (e != null && e.Item != null && e.Context != null)
            {
                if (ItemRecognized != null)
                {
                    OnUnknownItemDetected(this, e);
                }
            }
        }

        void context_OnItemRecognized(object sender, ItemAddedEventArgs e)
        {
            if (e!=null && e.Item != null && e.Context != null)
            {
                if (ItemRecognized != null)
                {
                    ItemRecognized(this, e);
                }
            }
        }



        public UMLTouchModel()
        {
            _views = new List<IUMLTouchView>();
            _contexts = new List<IDiagramContext>();
        }



    }
}

